using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QueryBuilder.Core.Query.Presenters;
using QueryBuilder.Core.Query.Type;

namespace QueryBuilder.Core.Query.Tests.ExampleConstraintPresenter.Mocks
{
    public class ToStringExamplePresenter : AbstractPresenter
    {
        public ToStringExamplePresenter(TypeMappingsRegistry typeMappingsRegistry) : base(typeMappingsRegistry)
        {
        }

        public override void Present(Aggregate aggregate)
        {
            throw new NotImplementedException();
        }
    }

    [TestClass]
    public class ToStringSqlQueryPresenter
    {
        private ToStringExamplePresenter converter;
        private ExampleTypeFactory typeFactory;

        [TestInitialize]
        public void SetUp()
        {
            this.typeFactory = new ExampleTypeFactory();
            this.converter = new ToStringExamplePresenter(this.typeFactory);
        }

        [TestMethod]
        public void ConvertToString_SqlQueryWithTwoColumnsAndTwoTables()
        {
            ITable table1 = new Table("table1");
            ITable table2 = new Table("table2");

            var column1 = new Column(table1, "id");
            var column2 = new Column(table2, "id");

            var sqlQuery = new SqlQuery();
            Constraint constraint = new Expression(Comparisons.Equals, column1, column2);

            sqlQuery.Constraint = constraint;

            sqlQuery.AddSelectSource(column1);
            sqlQuery.AddSelectSource(column2);

            sqlQuery.AddFrom(table1);
            sqlQuery.AddFrom(table2);

            string result = this.converter.ConvertToString(sqlQuery);

            Assert.AreEqual("SELECT table1.id,table2.id FROM table1,table2 WHERE (table1.id=table2.id)", result);
        }
    }
}